package com.changgou.system.search.service.impl;


import com.alibaba.fastjson.JSON;
import com.changgou.system.goods.feign.SkuFeign;
import com.changgou.system.goods.pojo.Sku;
import com.changgou.system.search.dao.ESManagerMapper;
import com.changgou.system.search.pojo.SkuInfo;
import com.changgou.system.search.service.EsManagerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
public class EsManagerServiceImpl implements EsManagerService {
    @Autowired
     private ElasticsearchTemplate elasticsearchTemplate;
      @Autowired
      private SkuFeign skuFeign;
      @Autowired
 private ESManagerMapper esManagerMapper;
    @Override
    public void createIndexAndMapping() {
        //创建索引
      elasticsearchTemplate.createIndex(SkuInfo.class);
        //创建映射
        elasticsearchTemplate.putMapping(SkuInfo.class);

    }

    @Override
    public void importAll() {
        //查询所有sku
        List<Sku> skuList = skuFeign.findSkuListBySpuId("all");
        if(skuList==null||skuList.size()<=0){
            throw new RuntimeException("当前没有查询出数据，无法导入索引库！");
        }

        List<SkuInfo> skuInfoList =new ArrayList<>();
        for (Sku sku : skuList) {
            SkuInfo skuInfo=new SkuInfo();
            BeanUtils.copyProperties(sku,skuInfo);
            skuInfo.setId(Long.parseLong(sku.getId()));

            String spec = skuInfo.getSpec();
            if(StringUtils.isEmpty(spec)){
                spec="{}";
            }
            Map map = JSON.parseObject(spec, Map.class);
            skuInfo.setSpecMap(map);

            skuInfoList.add(skuInfo);
        }
        esManagerMapper.saveAll(skuInfoList);
    }

    @Override
    public void importDataToESBySpuId(String spuId) {
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        if (skuList == null || skuList.size()<=0){
         throw new RuntimeException("当前商品无数据,无法导入索引库: "+spuId);
        }
        List<SkuInfo> skuInfoList =new ArrayList<>();
        for (Sku sku : skuList) {
            SkuInfo skuInfo=new SkuInfo();
            BeanUtils.copyProperties(sku,skuInfo);
            skuInfo.setId(Long.parseLong(sku.getId()));
            String spec = skuInfo.getSpec();
            if(StringUtils.isEmpty(spec)){
                spec="{}";
            }
            Map map = JSON.parseObject(spec, Map.class);
             skuInfo.setSpecMap(map);

             skuInfoList.add(skuInfo);
        }
     esManagerMapper.saveAll(skuInfoList);
    }

    @Override
    public void delDataBySpuID(String spuId) {

        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        if(skuList==null||skuList.size()<=0){
            throw new RuntimeException("当前没有查询出数据，无法导入索引库！");
        }

        for (Sku sku : skuList) {
            esManagerMapper.deleteById(Long.parseLong(sku.getId()));
        }
    }
}
